﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using SLLottery.LotterySrv;
using SLLottery.Common;
using System.Collections.ObjectModel;
using SLLottery.Controls;
using System.Windows.Threading;
using System.Threading;
namespace SLLottery.Controls
{
    public partial class SureBet : ChildWindow
    {
        private LotteryServiceClient srv;
        private DispatcherTimer timer = new DispatcherTimer();
        private bool? isShow;
        public SureBet(bool isShow)
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            timer.Interval = TimeSpan.FromSeconds(10);
            timer.Tick += new EventHandler(timer_Tick);
            this.Loaded += new RoutedEventHandler(SureBet_Loaded);
            srv.InsertBetRecordCompleted += new EventHandler<InsertBetRecordCompletedEventArgs>(srv_InsertBetRecordCompleted);
            srv.InsertBetRecordListCompleted += new EventHandler<InsertBetRecordListCompletedEventArgs>(srv_InsertBetRecordListCompleted);
            this.isShow = isShow;
        }
        public bool isReturn = true;
        void srv_InsertBetRecordListCompleted(object sender, InsertBetRecordListCompletedEventArgs e)
        {
            border_Processing.Visibility = Visibility.Collapsed;
            sp_button.Visibility = Visibility.Visible;
            border_Messager.Visibility = Visibility.Visible;
            OKButton.Visibility = Visibility.Collapsed;
            CancelButton.Visibility = Visibility.Collapsed;
            CloseButton.Visibility = Visibility.Visible;
            dataForm1.Visibility = Visibility.Collapsed;
            isXiaZhuEnd = true;
            RefreshUI();
            if (e.Result > 0)
            {
                MainPage.mainBet.RefreshGrid();
            }
            else
            {
                lblMsg.Text = "数据异常或者服务器繁忙，下注失败!";
                //Messager.Show("数据异常或者服务器繁忙，下注单无法提交到服务器!");
            }
            //this.DialogResult = true; 
        }

        void srv_InsertBetRecordCompleted(object sender, InsertBetRecordCompletedEventArgs e)
        {
            border_Processing.Visibility = Visibility.Collapsed;
            sp_button.Visibility = Visibility.Visible;
            isXiaZhuEnd = true;
            border_Messager.Visibility = Visibility.Visible;
            OKButton.Visibility = Visibility.Collapsed;
            CancelButton.Visibility = Visibility.Collapsed;
            CloseButton.Visibility = Visibility.Visible;
            dataForm1.Visibility = Visibility.Collapsed;
            isXiaZhuEnd = true;
            RefreshUI();
            if (e.Result > 0)
            {
                MainPage.mainBet.RefreshGrid();
            }
            else
            {
                lblMsg.Text = "数据异常或者服务器繁忙，下注失败!";
                //Messager.Show("数据异常或者服务器繁忙，下注单无法提交到服务器!");
            }
            //this.DialogResult = true; 
        }

        void SureBet_Loaded(object sender, RoutedEventArgs e)
        {

        }
        private BetRecord record;
        public SureBet(BetRecord betRecord, bool isShow)
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            timer.Interval = TimeSpan.FromSeconds(10);
            timer.Tick += new EventHandler(timer_Tick);
            this.Loaded += new RoutedEventHandler(SureBet_Loaded);
            srv.InsertBetRecordCompleted += new EventHandler<InsertBetRecordCompletedEventArgs>(srv_InsertBetRecordCompleted);
            srv.InsertBetRecordListCompleted += new EventHandler<InsertBetRecordListCompletedEventArgs>(srv_InsertBetRecordListCompleted);
            record = betRecord;
            this.isShow = isShow;
        }
        private List<BetRecord> recordList;
        public SureBet(List<BetRecord> betRecord, bool isShow)
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            timer.Interval = TimeSpan.FromSeconds(10);
            timer.Tick += new EventHandler(timer_Tick);
            this.Loaded += new RoutedEventHandler(SureBet_Loaded);
            srv.InsertBetRecordCompleted += new EventHandler<InsertBetRecordCompletedEventArgs>(srv_InsertBetRecordCompleted);
            srv.InsertBetRecordListCompleted += new EventHandler<InsertBetRecordListCompletedEventArgs>(srv_InsertBetRecordListCompleted);
            recordList = betRecord;
            this.isShow = isShow;
        }
        private bool isXiaZhuEnd = true;
        private int TimeOut = 60;
        void timer_Tick(object sender, EventArgs e)
        {
            if (isXiaZhuEnd == true)
            {
                TimeOut = 60;
                timer.Stop();
            }
            else
            {
                TimeOut--;
                if (TimeOut <= 0)
                {
                    isXiaZhuEnd = true;
                    Messager.Show("网络故障,无法连接到服务器");
                }
            }
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (MainPage.kaijiangState == 2)
            {
                isReturn = false;
                Messager.Show("已经封单，此时不能再下注!");
                this.DialogResult = true;
                return;
            }
            if (MainPage.kaijiangState == 3)
            {
                isReturn = false;
                Messager.Show("正在开奖中，此时不能再下注!");
                this.DialogResult = true;
                return;
            }
            border_Processing.Visibility = Visibility.Visible;
            sp_button.Visibility = Visibility.Collapsed;
            if (record != null)
            {
                record.IssueNo = (App.Session[Constant.CURRENTISSUENO_SESSION] as LotteryNumsInfo).IssueNo;
                srv.InsertBetRecordAsync(record);
            }
            else
            {
                foreach (BetRecord dr in recordList)
                {
                    dr.IssueNo = (App.Session[Constant.CURRENTISSUENO_SESSION] as LotteryNumsInfo).IssueNo;
                }
                ObservableCollection<BetRecord> tObjectStruct = new ObservableCollection<BetRecord>(recordList);
                srv.InsertBetRecordListAsync(tObjectStruct);
            }
            //跟进下注是否成功
            isXiaZhuEnd = false;
            //timer.Start();
            //=======

        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void dataForm1_Loaded(object sender, RoutedEventArgs e)
        {
            if (record != null)
                this.dataForm1.CurrentItem = record;
            else
            {
                BetRecord bet = new BetRecord();
                bet.BetCount = recordList.Count;
                bet.EachNumsMoney = recordList[0].EachNumsMoney;
                bet.BetMoney = recordList.Sum(p => p.BetMoney);
                bet.PlayTypeName = recordList[0].PlayTypeName;
                bet.Direction = recordList[0].Direction;
                this.dataForm1.CurrentItem = bet;
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
        /// <summary>
        /// 刷新界面
        /// </summary>
        public void RefreshUI()
        {
            if (isReturn == true)
            {
                LotteryServiceClient _proxy = WcfProxy.GetProxy;
                _proxy.GetCapitalInfoCompleted += (s, er) =>
                {
                    MainPage.PersonalControl.DataContext = er.Result;
                };
                _proxy.GetCapitalInfoAsync(App.Session[Constant.USERNAME_SESSION].ToString());
                if (!isShow.Value)
                    RefreshRateData();
            }
        }
        void RefreshRateData()
        {
            if (MainPage.lotteryTypeAll == LotteryType.NSSC//绑定逆向赔率
                || MainPage.lotteryTypeAll == LotteryType.N3D
                || MainPage.lotteryTypeAll == LotteryType.NSSQ)
            {
                LotteryServiceClient _srv = WcfProxy.GetProxy;
                _srv.GetOddsAsync(MainPage.playType, MainPage.playTypeRadio, (App.Session[Constant.CURRENTISSUENO_SESSION] as LotteryNumsInfo).IssueNo);
                _srv.GetOddsCompleted += (s, e) =>
                {
                    if (MainPage.rateGrids != null)
                    {
                        foreach (RateGrid rg in MainPage.rateGrids)
                        {
                            rg.DataSource = e.Result.ToList();
                            rg.BindData();
                        }
                    }
                    App.Session[Constant.REVERSEODDS_SESSION] = e.Result.ToList();
                };
            }
        }
    }
}

